#include "vehiclepage.h"
#include "mainwindow.h"
#include "ui_vehiclepage.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include <QRegularExpression>
#include <QMessageBox>
#include <QDateTime>
#include <QShortcut>
#include "protocol.h"
VehiclePage::VehiclePage(QWidget *parent,QTcpSocket* externalSocket) :
    QWidget(parent),
    ui(new Ui::VehiclePage),
     socket(externalSocket)
{
    ui->setupUi(this);
    ui->horizontalLayout->setContentsMargins(20, 20, 20, 20);
    ui->horizontalLayout_2->setContentsMargins(20, 20, 20, 20);
    ui->lineEdit->setMinimumWidth(200);
    ui->lineEdit_2->setMinimumWidth(200);

    QShortcut* shortcut = new QShortcut(QKeySequence(Qt::Key_Return), this);
    connect(shortcut, &QShortcut::activated, this, &VehiclePage::onPushButtons);
    connect(shortcut, &QShortcut::activated, this, &VehiclePage::onPushButtons);
}

VehiclePage::~VehiclePage()
{
    delete ui;
}

void VehiclePage::on_pushButton_in_car_clicked()
{

    if (!socket) {
        qDebug() << "Socket 是空的！";
        return;
    }

    const QString plate = ui->lineEdit->text().trimmed(); // 去除空格
    if (!isValidLicensePlate(plate)) {
        QMessageBox::critical(this, "非法车牌", "请输入正确的7位燃油车牌或8位新能源车牌！");
        return;
    }
    ui->lineEdit->setText("");
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString intime = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
    qDebug() << "入库时间：" << intime;

    // 创建JSON对象
    QJsonObject root;
    QJsonObject message;
    message["plate"] = plate;
    message["time"] = intime;
    root["func"] = 5;
    root["message"] = message;
    QByteArray dataToSend = Encode(root);
    socket->write(dataToSend);
}

void VehiclePage::on_pushButton_out_car_clicked()
{

    if (!socket) {
        qDebug() << "Socket 是空的！";
        return;
    }
    const QString plate = ui->lineEdit_2->text().trimmed();
    if (!isValidLicensePlate(plate)) {
        QMessageBox::critical(this, "非法车牌", "请输入正确的7位燃油车牌或8位新能源车牌！");
        return;
    }
    ui->lineEdit_2->setText("");
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString outtime = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
    qDebug() << "出库时间：" << outtime;

    QJsonObject root;
    QJsonObject message;
    message["plate"] = plate;
    message["time"] = outtime;
    root["func"] = 6;
    root["message"] = message;
    QByteArray dataToSend = Encode(root);
    socket->write(dataToSend);
}


void VehiclePage::handleServerMessage(const QJsonObject& message1)
{

    QJsonObject message2 = message1["message2"].toObject();
    int func2 = message1["func2"].toInt();
    qDebug()<<"VehiclePage::handleServerMessage,func2:"<<func2<<"message2:"<<message2;
    if(func2==1)
    {
        handleIncarMessage(message2);
    }
    else
    {
        handleOutncarMessage(message2);
    }


}

void VehiclePage::handleIncarMessage(const QJsonObject& message2)
{

    //qDebug() <<"VehiclePage::handleServerMessage,message2:"<<message2;
    int isReserved = message2["isReserved"].toInt();
    QString location = message2["location"].toString();
    qDebug() << "isReserved：" << isReserved <<"location"<<location;
    QMessageBox *msg = new  QMessageBox(this);
    msg->setWindowTitle("欢迎停车！");
    if(isReserved)
        msg->setText("为预约车辆，请前往"+location);
    else
        msg->setText("为非预约车辆，请前往"+location);
    msg->setIcon(QMessageBox::Information);
    msg->show();

}
void VehiclePage::handleOutncarMessage(const QJsonObject& message2)
{
    int isIn = message2["isIn"].toInt();
    if(isIn == 0)
    {
        QMessageBox *msg = new  QMessageBox(this);
        msg->setWindowTitle("提示");
        msg->setIcon(QMessageBox::Warning);
        msg->setText("您当前输入的车牌不在库中");
        msg->show();
        return;
    }
    else
    {
        int hours = message2["hours"].toInt();
        int mins = message2["mins"].toInt();
        int secs = message2["secs"].toInt();
        double fee = message2["fee"].toDouble();
        qDebug()<<"hours:"<<hours<<" mins:"<<mins<<" secs:"<<secs<<" fee:"<<fee;
        QMessageBox *msg = new  QMessageBox(this);
        msg->setWindowTitle("欢迎再次光临！");
        msg->setIcon(QMessageBox::Information);
        QString str = "您已停车"+QString::number(hours)+"小时，"+QString::number(mins)+"分钟， "+QString::number(secs)+"秒，您需要支付："+QString::number(fee)+"￥";
        msg->setText(str);
        msg->show();
    }
}

void VehiclePage::onPushButtons()
{
    if (!ui->lineEdit->text().trimmed().isEmpty()) {
        ui->pushButton_in_car->click();
    } else if (!ui->lineEdit_2->text().trimmed().isEmpty()) {
        ui->pushButton_out_car->click();
    }
}


QSet<QString> VehiclePage::getValidPrefixes() {
    QSet<QString> prefixes;
    prefixes << "京A" << "京B" << "京C" << "京E" << "京F" << "京G" << "京H" << "京J" << "京K" << "京L" << "京M" << "京N" << "京O" << "京P" << "京Q" << "京Y";
    prefixes << "津A" << "津B" << "津C" << "津D" << "津E" << "津F" << "津G" << "津H" << "津J" << "津K" << "津L" << "津M" << "津N" << "津Q" << "津R";
    prefixes << "沪A" << "沪B" << "沪C" << "沪D" << "沪E" << "沪F" << "沪G" << "沪H" << "沪J" << "沪K" << "沪L" << "沪M" << "沪N";
    prefixes << "渝A" << "渝B" << "渝C" << "渝F" << "渝G" << "渝H";
    prefixes << "冀A" << "冀B" << "冀C" << "冀D" << "冀E" << "冀F" << "冀G" << "冀H" << "冀J" << "冀R" << "冀T";
    prefixes << "晋A" << "晋B" << "晋C" << "晋D" << "晋E" << "晋F" << "晋H" << "晋J" << "晋K" << "晋L" << "晋M";
    prefixes << "蒙A" << "蒙B" << "蒙C" << "蒙D" << "蒙E" << "蒙F" << "蒙G" << "蒙H" << "蒙J" << "蒙K" << "蒙L" << "蒙M";
    prefixes << "辽A" << "辽B" << "辽C" << "辽D" << "辽E" << "辽F" << "辽G" << "辽H" << "辽J" << "辽K" << "辽L" << "辽M" << "辽N" << "辽P";
    prefixes << "吉A" << "吉B" << "吉C" << "吉D" << "吉E" << "吉F" << "吉G" << "吉H" << "吉J";
    prefixes << "黑A" << "黑B" << "黑C" << "黑D" << "黑E" << "黑F" << "黑G" << "黑H" << "黑J" << "黑K" << "黑L" << "黑M" << "黑N" << "黑P";
    prefixes << "苏A" << "苏B" << "苏C" << "苏D" << "苏E" << "苏F" << "苏G" << "苏H" << "苏J" << "苏K" << "苏L" << "苏M" << "苏N";
    prefixes << "浙A" << "浙B" << "浙C" << "浙D" << "浙E" << "浙F" << "浙G" << "浙H" << "浙J" << "浙K" << "浙L";
    prefixes << "皖A" << "皖B" << "皖C" << "皖D" << "皖E" << "皖F" << "皖G" << "皖H" << "皖J" << "皖K" << "皖L" << "皖M" << "皖N" << "皖P" << "皖Q" << "皖R" << "皖S";
    prefixes << "闽A" << "闽B" << "闽C" << "闽D" << "闽E" << "闽F" << "闽G" << "闽H" << "闽J" << "闽K";
    prefixes << "赣A" << "赣B" << "赣C" << "赣D" << "赣E" << "赣F" << "赣G" << "赣H" << "赣J" << "赣K" << "赣L" << "赣M";
    prefixes << "鲁A" << "鲁B" << "鲁C" << "鲁D" << "鲁E" << "鲁F" << "鲁G" << "鲁H" << "鲁J" << "鲁K" << "鲁L" << "鲁M" << "鲁N" << "鲁P" << "鲁Q" << "鲁R" << "鲁S" << "鲁U" << "鲁V" << "鲁Y";
    prefixes << "豫A" << "豫B" << "豫C" << "豫D" << "豫E" << "豫F" << "豫G" << "豫H" << "豫J" << "豫K" << "豫L" << "豫M" << "豫N" << "豫P" << "豫Q" << "豫R" << "豫S" << "豫U";
    prefixes << "鄂A" << "鄂B" << "鄂C" << "鄂D" << "鄂E" << "鄂F" << "鄂G" << "鄂H" << "鄂J" << "鄂K" << "鄂L" << "鄂M" << "鄂N" << "鄂P" << "鄂Q" << "鄂R";
    prefixes << "湘A" << "湘B" << "湘C" << "湘D" << "湘E" << "湘F" << "湘G" << "湘H" << "湘J" << "湘K" << "湘L" << "湘M" << "湘N" << "湘U";
    prefixes << "粤A" << "粤B" << "粤C" << "粤D" << "粤E" << "粤F" << "粤G" << "粤H" << "粤J" << "粤K" << "粤L" << "粤M" << "粤N" << "粤P" << "粤Q" << "粤R" << "粤S" << "粤T" << "粤U" << "粤V" << "粤W" << "粤X" << "粤Y" << "粤Z";
    prefixes << "桂A" << "桂B" << "桂C" << "桂D" << "桂E" << "桂F" << "桂G" << "桂H" << "桂J" << "桂K" << "桂L" << "桂M" << "桂N" << "桂P" << "桂R";
    prefixes << "琼A" << "琼B" << "琼C" << "琼D" << "琼E";
    prefixes << "川A" << "川B" << "川C" << "川D" << "川E" << "川F" << "川H" << "川J" << "川K" << "川L" << "川M" << "川Q" << "川R" << "川S" << "川T" << "川U" << "川V" << "川W" << "川X" << "川Y" << "川Z";
    prefixes << "贵A" << "贵B" << "贵C" << "贵D" << "贵E" << "贵F" << "贵G" << "贵H";
    prefixes << "云A" << "云C" << "云D" << "云E" << "云F" << "云G" << "云H" << "云J" << "云K" << "云L" << "云M" << "云N" << "云P" << "云Q" << "云R" << "云S";
    prefixes << "藏A" << "藏B" << "藏C" << "藏D" << "藏E" << "藏F" << "藏G" << "藏H";
    prefixes << "陕A" << "陕B" << "陕C" << "陕D" << "陕E" << "陕F" << "陕G" << "陕H" << "陕J" << "陕K" << "陕V";
    prefixes << "甘A" << "甘B" << "甘C" << "甘D" << "甘E" << "甘F" << "甘G" << "甘H" << "甘J" << "甘K" << "甘L" << "甘M" << "甘N";
    prefixes << "青A" << "青B" << "青C" << "青D" << "青E" << "青F" << "青G" << "青H";
    prefixes << "宁A" << "宁B" << "宁C" << "宁D" << "宁E";
    prefixes << "新A" << "新B" << "新C" << "新D" << "新E" << "新F" << "新G" << "新H" << "新J" << "新K" << "新L" << "新M" << "新N" << "新P";
    prefixes << "港A" << "澳A";
    return prefixes;
}


bool VehiclePage::isValidLicensePlate(const QString& plate) {
    QSet<QString> validPrefixes = getValidPrefixes();

    if (plate.length() != 7 && plate.length() != 8) {
        return false; // 长度必须是7或8位
    }

    QString prefix = plate.left(2); // 取前缀（省份+字母）
    if (!validPrefixes.contains(prefix)) {
        return false; // 非法前缀
    }

    // 正则表达式
    QRegularExpression fuelPattern("^[\u4e00-\u9fa5][A-Z][A-Z0-9]{5}$");           // 燃油车牌：7位
    QRegularExpression newEnergyPattern("^[\u4e00-\u9fa5][A-Z][A-HJ-NP-Z0-9]{6}$"); // 新能源车牌：8位

    if (plate.length() == 7) {
        return fuelPattern.match(plate).hasMatch();
    } else {
        return newEnergyPattern.match(plate).hasMatch();
    }
}














